#include <pthread.h>
#include <functional>
#include <string>
#include <iostream>

// function可以实现泛型
template <class T>
using func_t = std::function<void(T &)>;

namespace zhong
{

    // 类区
    template <class T>
    class Thread
    {
    private:
        static void *ThreadFunc(void *args)
        {
            Thread *t = (Thread *)args;

            t->_func(t->_data);

            return nullptr;
        }

    public:
        Thread(func_t<T> func, const T &data)
            : _func(func), _data(data), _isrunning(false) 
            {
            }
        ~Thread() {}

        bool Start()
        {
            int n = pthread_create(&_tid, nullptr, ThreadFunc, this);
            if (n == 0)
            {
                _isrunning = true;
                return true;
            }
            return false;
        }
        bool Join()
        {
            if (IsRunning() == false)
                return true;
            int n = pthread_join(_tid, nullptr);
            if (n == 0)
            {
                _isrunning = false;
                return true;
            }
            return false;
        }

        bool IsRunning() { return _isrunning; }
        pthread_t GetTid() const { return _tid; }

    private:
        pthread_t _tid;
        func_t<T> _func;
        T _data;
        bool _isrunning;
    };
}